<template>
	<div>
		<van-popup v-model="showConfirmEmail">
			<div class="confirm-box">
				<div class="confirm-title"></div>

				<div class="confirm-content">
					<div class="title">{{$t('confirmemail.tip1')}}<br><span>{{$t('confirmemail.tip2')}}</span></div>

					<!-- <div v-if="userinfo.email && userinfo.email.indexOf('gmail.com') != -1">
						<div class="hint"> 
							{{$t('confirmemail.tip9')}}
							<span class="email">{{userinfo.email}}
							</span>
							{{$t('confirmemail.tip10')}}
						</div>
						<div>
							<g-signin-button :params="googleSignInParams" @success="googleLoginSuccess"
								@error="googleLoginError">
								<button class="primary-btn">{{$t('profile.Google')}}
								</button>
							</g-signin-button>
						</div>
						<div class="line"></div>
					</div> -->
					<div>
						<div class="hint">
							{{$t('confirmemail.tip3')}}
							<span class="email">{{userinfo.email}}<br>
								<span>({{$t('confirmemail.tip4')}})</span>
							</span>
						</div>
					</div>

					<div class="email" style="width: 50%;margin: auto;">
						<el-form :model="codeObj" :rules="rules" ref="ruleForm">
							<el-form-item prop="code"
								:rules="[
							{ required: true, message: $t('confirmemail.inputCode'), trigger: 'blur' },{ min:6, message: $t('confirmemail.pleasedigitcode'), trigger: 'blur' }]">
								<el-input type="text" v-model="codeObj.code"
									:placeholder="$t('confirmemail.digitcode')"></el-input>
							</el-form-item>
						</el-form>
					</div><button class="primary-btn" @click="ConfirmCode">{{$t('confirmemail.tip5')}}</button>
					<div class="line"></div><button class="resend-btn"
						@click="Resendthecode">{{$t('confirmemail.tip6')}}</button>
					<div class="help">{{$t('confirmemail.tip7')}}? <span
							@click="goPage('/contact')">{{$t('confirmemail.tip8')}}</span></div>
				</div>
				<div class="cancel" @click="showConfirmEmail = false">x</div>
			</div>
		</van-popup>
		<!-- Congrats! -->
		<!-- Your account is now active. ￥500 has been put into your account! Start shopping and earning cash back today. -->
		<!-- Start Shopping -->
	</div>
</template>

<script>
	export default {
		data() {
			return {
				showConfirmEmail: false,
				codeObj: {
					code: '',
				},
				rules: {
					code: [{
						required: true,
						message: this.$i18n.locale == 'en-US' ? 'Please input email' : 'メールを入力してください',
						trigger: 'blur',
					}]
				},
				googleSignInParams: {
					client_id: "287675828063-a56etbrh9g73mmob94v6epeploc4s93g.apps.googleusercontent.com"
				},
			}
		},
		computed: {
			userStatus() {
				return this.$store.state.userinfo.status == 3 ? true : false
			},
			userinfo() {
				return this.$store.state.userinfo
			}
		},
		created() {},
		mounted() {
			this.$bus.on('showConfirmEmail', () => {
				this.showConfirmEmail = true
			})
			this.showConfirmEmail = this.userStatus
		},
		beforeDestroy() {
			this.$bus.off('showConfirmEmail')
		},
		methods: {
			goPage(url) {
				this.showConfirmEmail = false
				this.$router.push(url)
			},
			ConfirmCode() {
				this.$refs.ruleForm.validate((valid) => {
					if (valid) {
						this.$axios({
							url: 'user/siteUser/activateUser',
							params: {
								type: 1,
								verificationCode: this.codeObj.code
							}
						}).then(res => {
							this.showConfirmEmail = false
							this.getUserInfo()
							this.showToast('Binding success', 'バインド成功')
						})
					}
				})
			},
			Resendthecode() {
				console.log('重新发送验证码')
				this.$loading()
				this.$axios({
					url: 'user/siteUser/sendActivateEmail',
				}).then(res => {
					this.showToast('resend  successfully', '再送信に成功しました')
				})
			},
			getUserInfo() {
				this.$axios({
					url: '/user/siteUser/info',
					headers: {
						'Authorization': 'Bearer ' + this.$store.state.token
					}
				}).then(res => {
					console.log(res)
					this.$store.commit('SET_USERINFO', res.data)
					this.userEveryDay()
					setTimeout(() => {
						
						
						this.$router.replace('/shop')
					}, 1500)
				})
			},
			//google授权登录
			googleLoginSuccess(googleUser) {
				console.log('授权登录') 
				let googleinfo = this.$tool.initGoogleData(googleUser)
				this.$loading()
				console.log(googleinfo)
				this.$axios({
					url: 'user/siteUser/activateUser',
					method:'put',
					params:{
						type:2,
						googleOpenId:googleinfo.googleOpenId
					}
				}).then(res => {
					console.log(res)
					// this.showToast('resend  successfully', '再送信に成功しました')
				})
			},
			googleLoginError(error) {
				console.log(error)
			},
		},
		components: {},
		watch: {
			$route(val) {
				console.log(this.userStatus)
				this.showConfirmEmail = this.userStatus
			}
		}
	}
</script>

<style lang='scss' scoped>
	.confirm-box .confirm-content .hint {
		font-size: .7rem;
		color: #757575;
		margin: 15px 0;
	}

	.confirm-box .confirm-content .hint .email {
		text-decoration: underline;
		color: #000;
	}

	.confirm-box {
		width: 540px;
		position: relative;

		@media screen and (max-width: 900px) {
			width: 16rem;
		}
	}

	.confirm-box .confirm-title {
		padding: 0 80px 0 20px;
		height: 42px;
		line-height: 42px;
		border-bottom: 1px solid #eee;
		font-size: 14px;
		color: #333;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		background: #ff5900;
	}

	.confirm-box .confirm-content {
		padding: 20px;
		line-height: 24px;
		word-break: break-word;
		font-size: 14px;
		overflow: auto;
		box-sizing: border-box;
		text-align: center;
	}

	.confirm-box .cancel {
		position: absolute;
		cursor: pointer;
		right: 17px;
		top: 10px;
		font-size: 1rem;
		color: #757575;
	}

	.confirm-box .confirm-content .title {
		font-size: 20px;
		color: #ffaf52;
		margin: 15px 0;
	}

	.confirm-box .confirm-content .help {
		font-size: .6rem;
		color: #757575;
		margin-top: 20px;
	}

	.confirm-box .resend-btn {
		border: 1px solid #666;
		background-color: #fff;
		color: #595757;
		border-radius: 5px;
	}

	.confirm-box .line {
		border-top: 1px solid #ddd;
		width: 80%;
		margin: 0 auto;
	}

	.confirm-box .confirm-content .help span {
		color: #317cee;
		cursor: pointer;
		text-decoration: underline;
	}

	.confirm-box .primary-btn,
	.confirm-box .resend-btn {
		width: 80%;
		display: block;
		height: 32px;
		line-height: 32px;
		padding: 0 20px;
		font-size: .7rem;
		margin: .7rem auto;
	}

	.confirm-box .primary-btn {
		border: 1px solid #dedede;
		cursor: pointer;
		color: #fff;
		border-radius: 5px;
		border: none;
		background-color: #ff5900;
	}

	.confirm-box .confirm-content .hint {
		font-size: .7rem;
		color: #757575;
		margin: 15px 0;
	}

	.confirm-box .confirm-content .hint .email {
		text-decoration: underline;
		color: #000;
	}

	.confirm-box .confirm-content .hint span {
		color: #ff5900;
	}

	.confirm-box .confirm-content .title span {
		font-size: 18px;
		color: #ff5900;
	}
</style>
